from datetime import datetime

from airflow.models.idata_workflow import WorkFlow
from airflow.utils import timezone, dates


def save_dag2(name, start_time):
    start_id = "lb_1_{}".format(int(datetime.now().timestamp() * 1000))
    end_id  = "lb_2_{}".format(int(datetime.now().timestamp() * 1000))
    data = {
        "nodes":[
            {
                "x":439,
                "y":63,
                "name":"Start",
                "type":"START",
                "nodeId":"1",
                "stateId":"101003",
                "branches":[
                    {
                        "branchId":1,
                        "nextStateId":start_id,
                        "nextStateType":"end"
                    }
                ]
            },
            {
                "x":807,
                "y":177,
                "name":"sleep30",
                "type":"SHELL",
                "nodeId":1,
                "taskId":115,
                "stateId":start_id,
                "branches":[
                    {
                        "branchId":1,
                        "nextStateId":end_id,
                        "nextStateType":"end"
                    }
                ]
            },
            {
                "x":424,
                "y":252,
                "name":"End",
                "type":"END",
                "nodeId":"2",
                "stateId":end_id,
                "branches":[

                ]
            }
        ]
    }

    graph = WorkFlow(project_id=15, folder_id=82, name=name, content=data, owner='bin.lian', is_paused=False,cron='*/5 * * * *', start_time=start_time, update_time=start_time)

    graph.insert()
    graph.sync_dag()
    print(graph.content)

if __name__ == '__main__':
    start_time = dates.from_milliseconds(datetime.now().timestamp()*1000)
    for num in range(1, 401):
        save_dag2("name_{}".format(num),start_time)

